/**
 * Created by Administrator on 2017/7/27.
 */
angular.module('module.activity_list',[])
    .config(['$stateProvider',function ($stateProvider) {
        $stateProvider
            .state('activity_list',{
                params:{originatorId:null},
                url:'/activity_list',
                templateUrl:'view/activity_list.html',
                controller:'activity_list_controller'
            })
    }])
    .controller('activity_list_controller',["$scope",'$state',"$stateParams",'ActivityService','OriginatorService',function ($scope,$state,$stateParams,ActivityService,OriginatorService) {
        $(".datetimepicker").remove();
        console.log('activity_list_controller启动成功');
        var query_terms={
            // originatorId:$stateParams.originatorId,
            originatorId:1,
            state:0,
            pageNum:1
        };

//分页模块开始，该模块需要配置get_pagesum函数以及get_list_data_by_pagenum函数,并依赖angularjs以及util.controller.js(晓峰的工具函数库)-------------------------------------------------------------------------------------------------------------------

        // 页码部分初始化开始
        var pagesum;
        $scope.pagenum_list=[];
        var current_page=1;
        $scope.pagination_show=true;

        //获取总页数(该函数附带回调函数，用于解决异步问题)
        function get_pagesum(sfun,efun) {
            ActivityService.getActivityListSumByOriginatorOrStateOrBothAndPageNum(query_terms,function (data) {
                pagesum=data.ActiveListLength;
                if(sfun()){sfun()}
            },efun)
        }

        // 根据页数拿到该页题目列表函数
        var get_list_data_by_pagenum=function(pagenum){
            query_terms.pageNum=pagenum;
            $scope.Activity_list=ActivityService.getActivityListByOriginatorOrStateOrBothAndPageNum(query_terms);
        };

        //从这里开始与页面数据无关

        //配置pagenum_list函数
        var configure_pagenum_list=function(pagenum){
            get_pagesum(function(){
                $scope.pagenum_list=create_pagenum_list(pagesum,pagenum);
                $scope.pagination_show = pagesum == 0 || pagesum == 1;
            });
        };

        //刷新数据函数
        function refresh_data() {
            configure_pagenum_list(current_page);
            get_list_data_by_pagenum(current_page);
        }
        refresh_data();

        //页码交互事件

        //页码点击函数
        $scope.page_click=function(pagenum){
            current_page=pagenum;
            refresh_data();
        };

        //向前翻页函数
        $scope.page_up=function(){
            if(current_page>1){
                current_page=current_page-1;
                refresh_data();
            }else {
                alert("已经是第一页了")
            }
        };

        //向后翻页函数
        $scope.page_down=function(){
            if(current_page<pagesum){
                current_page=current_page+1;
                refresh_data();
            }else {
                alert("已经是最后一页了")
            }
        };

        // 分页模块结束------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

        $scope.set_state=function(state){
            query_terms.state=state;
            refresh_data();
        };

        $scope.originator={};
        $scope.originator.originatorId=1;
        // $scope.originator.originatorId=$stateParams.originatorId;
        OriginatorService.getOriginatorByOriginatorId({originatorId:$scope.originator.originatorId},function (data) {
            $scope.originator=data;
            console.log($scope.originator)
        });

        $scope.updateOriginator=function(){
            console.log(106);
            $scope.originator.originatorState=0;
            OriginatorService.updateOriginator($scope.originator,function (data) {
                console.log("修改组织者信息成功",data);
            },function (data) {
                console.log("修改组织者信息失败",data);
            })
        };

        $scope.addActivity=function(){
            switch ($scope.originator.originatorState)
            {
                case 0:
                    alert('您的组织者信息尚未审核，请耐性等待审核通过再发起活动');
                    return;
                case 1:
                    $state.go('create_activity',{originatorId:$scope.originator.originatorId});
                    return;
                case 2:
                    alert('您的组织者身份申请未通过，请重新申请');
                    return;
            }
        }
    }]);